<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="指标名称" :labelCol="{span: 6}" :wrapperCol="{span: 14, offset: 1}">
              <j-input placeholder="请输入" v-model="queryParam.name"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="数据源名称" :labelCol="{span: 6}" :wrapperCol="{span: 14, offset: 1}">
              <a-select allowClear :getPopupContainer="trigger => trigger.parentNode" v-model="queryParam.dataSource" placeholder="请选择">
                  <a-select-option  :value="item.code" :key="item.code" v-for="item in dataSourceList">
                      {{ item.name }}
                  </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="5" :sm="8">
            <a-form-item label="指标分类" :labelCol="{span: 6}" :wrapperCol="{span: 14, offset: 1}">
              <a-tree-select
                  :getPopupContainer="trigger => trigger.parentNode"
                  show-search
                  allowClear
                  tree-node-filter-prop="title"
                  :replaceFields="replaceFields"
                  v-model="queryParam.catalogId"
                  style="width: 100%"
                  placeholder="请选择"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="indexCatalogs"
              />
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="8">
            <a-form-item label="描述" :labelCol="{span: 6}" :wrapperCol="{span: 14, offset: 1}">
              <j-input placeholder="请输入" v-model="queryParam.remark"></j-input>
            </a-form-item>
          </a-col>
          <a-col :md="5" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQueryBefore" icon="search">查询</a-button>
              <a-button type="primary" @click="searchResetBefore" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <j-super-query :showFlag="true" :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
          <a-menu-item key="2" @click="addSystem"><a-icon type="plus"/>增加分类</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">取消勾选</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x: true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="indexCatalogVoList" slot-scope="indexCatalogVoList">
          <a-tag
            style="font-size: 14px; margin-top: 5px; float: left;"
            v-for="(catalog, index) in indexCatalogVoList"
            :key="'catalog' + index"
            color="blue"
          >{{ catalog.catalogName }}</a-tag
          >
        </span>

        <span slot="dimensionInfoVoList" slot-scope="dimensionInfoVoList">
          <a-tag
            style="font-size: 14px; margin-top: 5px"
            v-for="(topic, index) in dimensionInfoVoList"
            :key="index"
            color="blue"
          >{{ topic.name }}</a-tag
          >
        </span>

        <template slot="customRenderStatus" slot-scope="status">
          <a-tag v-if="status === 1" color="green">启用</a-tag>
          <a-tag v-if="status === 0" color="red">停用</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm placement="topLeft" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
          <a-divider type="vertical" />
          <a @click="handleInterfaceList(record.id)">接口信息</a>
        </span>

      </a-table>
    </div>
    <add-index-to-system-modal ref="addIndexToSystemModal" @close="loadData" />
    <sc-situation-index-info-modal ref="modalForm" @ok="modalFormOk"></sc-situation-index-info-modal>
  </a-card>
</template>

<script>
import { deleteAction } from '@/api/manage'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ScSituationIndexInfoModal from './modules/ScSituationIndexInfoModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import AddIndexToSystemModal from './modules/AddIndexToSystemModal'

import { getAction } from '@/api/manage'
export default {
  name: 'ScSituationIndexInfoList',
  components: {
    ScSituationIndexInfoModal,
    AddIndexToSystemModal
  },
  mixins: [JeecgListMixin, mixinDevice],
  data() {
    return {
      topicType: null,
      dataSourceList: [],
      description: '指标信息表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '指标名称',
          align: 'center',
          dataIndex: 'name'
        },
        {
          title: '指标分类',
          align: 'center',
          dataIndex: 'indexCatalogVoList',
          scopedSlots: { customRender: 'indexCatalogVoList' }
        },
        {
          title: '维度',
          align: 'center',
          dataIndex: 'dimensionInfoVoList',
          scopedSlots: { customRender: 'dimensionInfoVoList' }
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          scopedSlots: { customRender: 'customRenderStatus' }
        },
        {
          title: '描述',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '配置名称',
          align: 'center',
          dataIndex: 'configId_dictText'
        },
        {
          title: '数据源',
          align: 'center',
          dataIndex: 'dataSource_dictText'
        },
        {
          title: '表名',
          align: 'center',
          dataIndex: 'tableName'
        },
        {
          title: '字段名',
          align: 'center',
          dataIndex: 'tableColumn'
        },
        {
          title: '数据类型',
          align: 'center',
          dataIndex: 'dataType_dictText'
        },
        {
          title: '数据长度',
          align: 'center',
          dataIndex: 'columnLength'
        },
        {
          title: '小数精度',
          align: 'center',
          dataIndex: 'scale'
        },
        {
          title: '统计单位',
          align: 'center',
          dataIndex: 'unit'
        },
        {
          title: '是否率值',
          align: 'center',
          dataIndex: 'isPercent_dictText'
          // scopedSlots: { customRender: 'isPercent' }
        },
        {
          title: '汇聚类型',
          align: 'center',
          dataIndex: 'groupType_dictText'
        },
        // {
        //   title: '状态',
        //   align: 'center',
        //   dataIndex: 'status'
        // },
        {
          title: '编辑人',
          align: 'center',
          dataIndex: 'updateBy'
        },
        {
          title: '编辑时间',
          align: 'center',
          dataIndex: 'updateTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/index/config/listIndex',
        delete: '/index/scSituationIndexInfo/delete',
        deleteBatch: '/index/scSituationIndexInfo/deleteBatch',
        exportXlsUrl: '/index/scSituationIndexInfo/exportXls',
        importExcelUrl: 'index/scSituationIndexInfo/importExcel'

      },
      dictOptions: {},
      superFieldList: [],
      isorter: {
        column: null,
        order: null
      },
      replaceFields: {
        children: 'childList',
        title: 'catalogName',
        key: 'id',
        value: 'id'
      },
      indexCatalogs: []
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  created() {
    this.getIndexCatalogs()
    this.getDataSourceList()
    this.getSuperFieldList()
  },
  methods: {
    handleInterfaceList(indexId) {
      this.$router.push({ path: '/modelManage/interface-index', query: { indexId: indexId }})
    },
    changeTopicType(val) {
      this.topicType = val.id
      this.searchQueryBefore()
    },
    searchQueryBefore() {
      this.searchQuery()
    },
    getIndexCatalogs() {
      this.indexCatalogs = []
      getAction('/index/config/queryIndexCatalog', {}).then((res) => {
        if (res.success) {
          // 部门全选后，再添加部门，选中数量增多
          this.indexCatalogs = res.result
        }
      })
    },
    searchResetBefore() {
      this.$refs.superQueryModal.handleReset()
      this.searchReset()
    },
    getDataSourceList() {
      this.dataSourceList = []
      getAction('/sys/dataSource/list', { pageNo: 1, pageSize: 10000 }).then((res) => {
        if (res.success) {
          res.result.records.map(item => {
            if (item.dbStatus === '1') this.dataSourceList.push(item)
          })
        }
      })
    },
    addSystem() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
      } else {
        this.$refs.addIndexToSystemModal.add(this.selectedRowKeys)
      }
    },
    // batchDel: function() {
    //   console.log(this.checkedKeys)
    //   if (this.checkedKeys.length <= 0) {
    //     this.$message.warning('请选择一条记录！')
    //   } else {
    //     var ids = ''
    //     for (var a = 0; a < this.checkedKeys.length; a++) {
    //       ids += this.checkedKeys[a] + ','
    //     }
    //     var that = this
    //     this.$confirm({
    //       title: '确认删除',
    //       content: '确定要删除所选中的 ' + this.checkedKeys.length + ' 条数据，以及子节点数据吗?',
    //       onOk: function() {
    //         deleteAction(that.url.deleteBatch, { ids: ids }).then((res) => {
    //           if (res.success) {
    //             that.$message.success(res.message)
    //             that.loadData()
    //             that.onClearSelected()
    //           } else {
    //             that.$message.warning(res.message)
    //           }
    //         })
    //       }
    //     })
    //   }
    // },
    handleDelete(id) {
      var that = this
      deleteAction(that.url.delete, { id: id }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
          that.onClearSelected()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      const fieldList = []
      fieldList.push({ type: 'string', value: 'name', text: '指标名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'remark', text: '描述', dictCode: '' })
      fieldList.push({ type: 'string', value: 'configId', text: '配置名称', dictCode: 'sc_situation_index_config, config_name, id' })
      fieldList.push({ type: 'string', value: 'dataSource', text: '数据源', dictCode: 'sys_data_source,name,code' })
      fieldList.push({ type: 'string', value: 'tableName', text: '表名', dictCode: '' })
      fieldList.push({ type: 'string', value: 'tableColumn', text: '字段名', dictCode: '' })
      fieldList.push({ type: 'int', value: 'dataType', text: '数据类型', dictCode: 'column_data_type' })
      fieldList.push({ type: 'int', value: 'columnLength', text: '数据长度', dictCode: '' })
      fieldList.push({ type: 'string', value: 'scale', text: '小数精度', dictCode: '' })
      fieldList.push({ type: 'string', value: 'unit', text: '统计单位', dictCode: '' })
      fieldList.push({ type: 'int', value: 'isPercent', text: '是否率值', dictCode: 'yn' })
      fieldList.push({ type: 'string', value: 'groupType', text: '汇聚类型', dictCode: 'index_group_type' })
      // fieldList.push({ type: 'int', value: 'status', text: '状态', dictCode: '' })
      fieldList.push({ type: 'string', value: 'updateBy', text: '编辑人', dictCode: '' })
      fieldList.push({ type: 'string', value: 'updateTime', text: '编辑时间', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
